<template>
	<view>
		<view class="mb30">
			<u-notice-bar color="#3dd3cb" bgColor="#dff4f4" :text="text"></u-notice-bar>
		</view>
		<view class="renzheng">
			<view class="content2" v-for="(item,index) in user_contract" :key="index">
				<view class="top1">
					紧急联系人{{index + 1}}
				</view>
				<!-- <view class="top2">
					<view class="zsname"> 真实姓名 </view>
					<input type="text" v-model="item.jjrealname" />
				</view> -->
				<view class="top3">
					<view class="name"> 姓名 </view>
					<view class="huis">
						<input type="text" v-model="item.contactName"  @input="handleChineseInput($event,item)" />
					</view>
				</view>
				<view class="top4">
					<view class="dianhua"> 电话 </view>
					<view class="contactPhone">
						<view>
							<input type="text" v-model="item.contactPhone" @input="phoneRegular($event,item)"/>
						</view>
					</view>
				</view>
				<view class="top5">
					<view class="contactRelation"> 关系 </view>
					<u-form>
						<u-form-item>
							<u-radio-group v-model="item.contactRelation" placement="row">
								<u-radio activeColor="#3dd3cb" :customStyle="{marginRight:'6rpx'}"
									v-for="($item, $index) in radiolist1" :key="$index" :label="$item.name"
									:name="$item.val" @change="(n)=>item.contactRelation = n">
								</u-radio>
							</u-radio-group>
						</u-form-item>
					</u-form>
				</view>
			</view>
		</view>
		<button class="bclxr2" @click="addlxr()">添加联系人</button>
		<button class="bclxr" @click="submitlxr()" style="margin-top: 10px;">保存联系人</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				text: "填写直系亲属会大幅提高审核通过率,请填写真实姓名，我们一般不可能会联系您的紧急联系人",
				radiolist1: [{
						name: '父母',
						val: 1,
						disabled: false
					},
					{
						name: '配偶',
						val: 5,
						disabled: false
					},
					{
						name: '其他亲属',
						val: 9,
						disabled: false
					}
				],
				user_contract: [{
					contactName: "",
					contactPhone: "",
					contactRelation: 1
				}]
			}
		},
		methods: {
			phoneRegular(e,item) {
				console.log(e)
				let value = e.detail.value;
				// 1. 过滤非数字字符
				item.contactPhone = value.replace(/[^\d]/g, '');
				e.target.value = value;
			},
			handleChineseInput(e,item) {
				let value = e.detail.value;
				// 1. 过滤非汉字字符（包括标点、数字、字母等）
				item.contactName = value.replace(/[^\u4e00-\u9fa5]/g, '');
				e.target.value = value
			},
			// 提交联系人
			submitlxr() {
				let self = this;
				// uni.showLoading({
				// 	title: '提交中,请勿离开页面'
				// });
				console.log(self.user_contract);
				let allPropertiesTruthy = () => {
					let allValid = true;
					self.user_contract.forEach(item => {
						for (let key in item) {
							if (item.hasOwnProperty(key) && !item[key]) {
								allValid = false;
								
								break;
							}
						}
					});
					return allValid;
				};
				if (!allPropertiesTruthy()) {
					uni.showToast({
						title: '请将信息填写完整!!',
						duration: 2000
					});
					return;
				
				}
				self.user_contract.forEach((item)=>{
					item.jjrealname = item.contactName
				})
				self._post('user.user/userContract', {
					params: JSON.stringify(self.user_contract)
				}, function(res) {
					if (res.code == 1) {
						uni.showToast({
							title: res.msg,
							duration: 2000
						});
						uni.navigateBack()
					} else {
						uni.showToast({
							title: "提交失败,请重新提交或联系客服!!",
							duration: 2000
						});
					}

				});
			},
			// 查询联系人
			getData() {
				let self = this;
				self._get(
					'user.index/detail', {},
					function(res) {
						console.log(res.data.userInfo, "成功返回");
						user_contract: Array(1)
						if (res.data.userInfo.user_contract.length > 0) {
							self.user_contract = []
							res.data.userInfo.user_contract.forEach(item => {
								let obj = {
									contactName: item.contactName,
									jjrealname: item.jjrealname,
									contactPhone: item.contactPhone,
									contactRelation: item.contactRelation
								}
								self.user_contract.push(obj)
							})
						}
					}
				);
			},
			addlxr() {
				let obj = {
					contactName: "",
					contactPhone: "",
					contactRelation: 1
				}
				this.user_contract.push(obj)
			},
		},
		onLoad() {
			this.getData();
		}
	}
</script>

<style lang="scss">
	.add {
		border-radius: 50rpx;
		padding: 10rpx 20rpx;
		text-align: center;
		// height: 30rpx;
		line-height: 32rpx;
		color: #3dd3cb;
		border: 1rpx solid #3dd3cb;
	}

	.bclxr {
		color: white;
		width: 582rpx;
		height: 88rpx;
		line-height: 80rpx;
		background: linear-gradient(360deg, #05C8BE 0%, #6FD9D7 100%);
		border-radius: 44rpx;
		margin: 0 auto;
	}

	.bclxr2 {
		color: white;
		background-color: #BF6FDC;
		width: 582rpx;
		height: 88rpx;
		line-height: 80rpx;
		border-radius: 44rpx;
		margin: 0 auto;
	}

	page {
		background-color: #F7F6FB;
	}

	.renzheng {
		// border-radius: 20rpx;
		// background-color: #FFFFFF;
		// margin: 20rpx;
	}

	.content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 25rpx;
		border-radius: 25rpx;
		margin: 20rpx;
		background-color: #FFFFFF;

		.left {
			view {
				margin: 10rpx;
			}

			.top {
				color: #2F3131;
				font-size: 34rpx;
				font-weight: 700;
			}

			.buttom {
				color: #d6d7d9;
				font-size: 24rpx;
			}
		}
	}

	.content2 {
		padding: 20rpx 25rpx;
		border-radius: 25rpx;
		margin: 20rpx;
		background-color: #FFFFFF;

		.top1 {
			color: #2F3131;
			font-size: 34rpx;
			font-weight: 700;
		}

		.top2 {
			display: flex;
			height: 50rpx;
			align-items: center;
			border-bottom: 1rpx solid #d6d7d9;
			padding: 25rpx 0;

			.zsname {
				font-size: 34rpx;
				margin-right: 20rpx;
			}
		}

		.top3 {
			display: flex;
			height: 50rpx;
			align-items: center;
			border-bottom: 1rpx solid #d6d7d9;
			padding: 25rpx 0;
			font-size: 34rpx;

			.name {
				margin-right: 20rpx;
			}

			.huis {
				color: #B0B0B0;
			}
		}

		.top4 {
			display: flex;
			height: 50rpx;
			align-items: center;
			border-bottom: 1rpx solid #d6d7d9;
			padding: 25rpx 0;
			font-size: 34rpx;

			.dianhua {
				margin-right: 20rpx;
			}

			.contactPhone {
				color: #B0B0B0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 85%;
			}
		}

		.top5 {
			display: flex;
			font-size: 34rpx;
			align-items: center;
			border-bottom: 1rpx solid #d6d7d9;

			.contactRelation {
				margin-right: 20rpx;
			}
		}
	}
</style>